package demo.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

//it's disgusting...
public class ContentFilter {
	
	private final static String REGEX = "@[A-Za-z0-9\u4e00-\u9fa5_-]+";
	private final static String LEFT = "<a href=\"http://127.0.0.1:8080/n/";
	private final static String CENTER = "\">";
	private final static String RIGHT = "</a>";
	
	
	private final static String REGEX_SEARCH = "@[<(\\w+)></(\\w+)>A-Za-z0-9\u4e00-\u9fa5_-]+";
	
	public static String getFiltratedContent(String rawContent) {
		Matcher matcher = Pattern.compile(REGEX).matcher(rawContent);
		StringBuffer stringBuffer = new StringBuffer();
		while (matcher.find()) {
			String keytmp = matcher.group();
			String defaultFormatKey = LEFT + keytmp.substring(1) + CENTER + keytmp + RIGHT;
			matcher.appendReplacement(stringBuffer, defaultFormatKey);
		}
		matcher.appendTail(stringBuffer);
		return stringBuffer.toString();
	}
	
	public static String getFiltratedContent(String rawContent, int i) {
		Matcher matcher = Pattern.compile(REGEX_SEARCH).matcher(rawContent);
		StringBuffer stringBuffer = new StringBuffer();
		while (matcher.find()) {
			String keytmp = matcher.group();
//System.out.println(keytmp);
			String srcString = keytmp.substring(1).replaceAll("<i>", "");
			srcString = srcString.replaceAll("</i>", "");
//System.out.println(srcString);
			String defaultFormatKey = LEFT + srcString + CENTER + keytmp + RIGHT;
			matcher.appendReplacement(stringBuffer, defaultFormatKey);
		}
		matcher.appendTail(stringBuffer);
		return stringBuffer.toString();
	}
	
	public static String getFiltratedContent(String rawContent, String regex) {
		Matcher matcher = Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(rawContent);
		StringBuffer stringBuffer = new StringBuffer();
		while (matcher.find()) {
			String keytmp = matcher.group();
			String defaultFormatKey = "<i>" + keytmp + "</i>";
			matcher.appendReplacement(stringBuffer, defaultFormatKey);
		}
		matcher.appendTail(stringBuffer);
		return getFiltratedContent(stringBuffer.toString(),0);
	}
	
	public static void main(String[] args) {
//		String string = "asdf@asdf“@;s阿斯顿:ASDFW @@-_@@-阿斯顿   啊@1____-----23";
//		System.out.println(getFiltratedContent(string));
		String string = "赵薇：拍了一整天很累，可是有很多好朋友在一起，哪里都亲切，爱心泛滥~@林心如 等你回来啊！哈哈 [爱你] ";
//		System.out.println(getFiltratedContent(string));
		System.out.println(getFiltratedContent((string), "心"));
//		System.out.println(getFiltratedContent(getFiltratedContent(string), "斯"));
//		System.out.println(getFiltratedContent(getFiltratedContent(string, "斯")));
	}
	
}
